Create interactive Candlestick charts of OHLCV data obtained from yfinance library using python libraries.
Visualization
Finance
Published
January 26, 2024
Introduction
In this post, we will be looking at creating candlestick charts, which are integral to traders’ daily routines, using Python libraries such as Mplfinance, Plotly and Bokeh, and look at customizing the plots, change themes and tweak some colors.
As this blog post is written on Quarto, it enables viewers to interact with the output plots. You’ll be able to pan and zoom with the charts created with plotly and bokeh!
So, now lets head on to fetch our data first.
Getting Data
We will make use of the yfinance library. Let’s fetch the Open-High-Low-Close and Volume data for Apple (AAPL) for the past 5 months.
Our data looks like this:
Code
import warningswarnings.simplefilter(action='ignore', category=FutureWarning)import yfinance as yfimport pandas as pddata = yf.download("AAPL", period="5mo",auto_adjust=True)data.head()
[*********************100%%**********************] 1 of 1 completed
Open
High
Low
Close
Volume
Date
2023-11-24
190.626752
190.656713
189.008822
189.727905
24048300
2023-11-27
189.677965
190.427010
188.659261
189.548126
40552600
2023-11-28
189.538144
190.836490
189.158623
190.157349
38415400
2023-11-29
190.656711
191.845197
188.729178
189.128662
43014200
2023-11-30
189.598056
190.077455
187.950165
189.707916
48794400
Plots
Now we will head straight into plotting. We will start with mplfinance, a special module for finance charts of matplotlib.
mplfinance
Creating candlestick charts with mplfinance is a simple one line code:
Code
import mplfinance as mpfmpf.plot(data, type="candle", volume=True, style="yahoo", figsize=(6,3),title="AAPL Candlestick with Mplfinance", ylabel="Price [USD]")
This plot uses the “yahoo” style. We can check the available style using the command mpf.available_styles(). Now let us look at a few other available styles below:
And now we will tweak and play with the candle colors and background. First we will set the base mplstyle to dark_background (for reference, check here). Then we will set marketcolors for the chart:
Code
mc = mpf.make_marketcolors(up="g",down="r",inherit=True)s = mpf.make_mpf_style(base_mpl_style="dark_background",marketcolors=mc)mpf.plot(data, type="candle", volume=True, style=s, figsize=(6,3),title="Customised Dark theme plot on mplfinance", ylabel="Price [USD]")
For looking further stye changes using mplfinance, one might check their example notebook
We will move ahead and try hands on some interactive plotting libraries.
Plotly
Plotly is perhaps the most used interactive visualization library in python. It offers a great deal of customization. With Plotly, we will make two types of charts, OHLC and Volume plotted together in first one, separate in the next.
Plotly as well offers multiple themes and customizations, for now will use the dark theme and just change the colors of volume bars for the first plot.
Bokeh is an interactive visualization library for modern web browsers. It provides elegant, concise construction of versatile graphics, and affords high-performance interactivity over large or streaming datasets. Bokeh can help anyone who would like to quickly and easily make interactive plots, dashboards, and data applications.
Bokeh additionally has great documentation and user guide. Now let’s look at a candlestick chart created using Bokeh:
That really looks good. If you are going through the code here, you can see how simple it would be to change the colors of bars. One can further add tooltips that will display the OHLC data on hovering over the candles.
One might further look at libraries like Altair, ggplot. One great library that I just found while writing this post is Holoviews